/*
 * Copyright 2006 Brian Watkins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */


package bw.news;

import javax.servlet.ServletContext;
import java.io.*;
import java.util.*;
import bw.util.*;
import bw.os.*;
import bw.refresh.*;

public class NewsServant implements Servant
{
    public static final String DO_BACKGROUND_NEWS_REFRESH = "doBackgroundNewsRefresh";

    public void init ( ServletContext context )
    {
        NewsManager manager = NewsManager.getInstance();
        
        //get the xml news file for the news manager
        //String newsFilePath = context.getRealPath( context.getInitParameter( "newsFile" ) );

        //the init method will parse the xml file if no categories were found in the
        //ObjectStore. Hacky. But not too bad since this is guaranteed to be the first place 
        //that deals with the NewsManager.
        //manager.init( new File( newsFilePath ) );
        
        //add to the refresherPool if necessary
        String shouldRefresh = context.getInitParameter( NewsServant.DO_BACKGROUND_NEWS_REFRESH );
        if ( "true".equalsIgnoreCase( shouldRefresh ) ) 
        {
            Config.setProperty( NewsServant.DO_BACKGROUND_NEWS_REFRESH, true );
            RefresherPool.getInstance().addSource( new NewsRefresherSource() );
        }
        else
        {
            Config.setProperty( NewsServant.DO_BACKGROUND_NEWS_REFRESH, false );
            Log.getInstance().write( "NewsServant: Background news refreshing is disabled." );
        }
    }
    
    public void shutdown ( ServletContext context )
    {
        //nothing
    }

}